<?php

namespace App\Admin\Controllers\Record;

use App\Http\Controllers\Controller;
use App\Admin\Models\Record\Consult;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use DB;

class ConsultController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('仪器咨询记录')
            ->description('列表')
            ->body($this->grid());
    }
    
    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('流水记录')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('账本')
            ->description('手工帐')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Consult);
        $grid->model()->where('sId', '=', Admin::user()->sId);
        $grid->rcId('ID');
        $grid->rcName('姓名');
        $grid->rcMobile('联系方式');
        $grid->rcSex('性别')->using(['0' => '女', '1' => '男']);
        $grid->rcAge('年龄');
        $grid->rcConsultItem('咨询项目');
        $grid->rcExperienceItem('体验项目');
        $grid->rcBuyTreatment('购买疗程');
        $grid->rcOtherCure('接收过的其它治疗');
        $grid->rcAllergy('过敏史');
        $grid->rcOftenTake('经常服用的药品/保健品');
        $grid->rcNowTake('正在服用的药品/保健品');
//      $grid->rcIsSkinInfection('皮肤是否感染')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsOpenWounds('有无开放伤')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsPregnancy('是否怀孕')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsRecentExposure('近期被日光暴晒')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsAvoidExposure('治疗后能否避免日光暴晒')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsSkinDisease('是否患有癫痫')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsScarSkin('是否疤痕体质')->using(['0' => '否', '1' => '是']);
//      $grid->rcIsHirsutism('是否先天性多毛')->using(['0' => '否', '1' => '是']);
//      $grid->rcRecordDate('登记时间');
//      $grid->rcRecordName('登记人');
//      $grid->createTime('创建时间');
//      $grid->createUser('创建人');

		
		$grid->actions(function ($actions) {
			
		    $actions->disableDelete();
		    $actions->disableEdit();
			$actions->disableView();
		});

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();
		    

		});
		
		// 禁用新增按钮
//		$grid->disableCreateButton();

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出
		$grid->disableExport();
        return $grid;
    }
    
    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$walletTrade = new walletTrade();
        $show = new Show($walletTrade->findOrFail($id));

        //$show->spId('ID');
        $show->spType('参数类型');
        $show->spKey('参数名称');
        $show->spValue('参数值');
        $show->spMemo('参数备注');
        $show->spSort('参数排序');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(Consult::class, function (Form $form) {
			
			$now = date('YmdHis', time());
			$today = date('Y-m-d', time());
			
			$form->text('rcName', '姓名');
			$form->mobile('rcMobile', '联系方式')->rules('required|regex:/^[1][0-9]{10}$/', [
        									'required' => '请填写手机号',
        									'regex' => '手机号格式不正确'
        									])
        									->attribute(['style' => 'width: 25%']);
			$form->select('rcSex', '性别')->options(array('0'=> '女', '1' => '男'));
			$form->text('rcAge', '年龄');
			$form->text('rcConsultItem', '咨询项目');
			$form->text('rcExperienceItem', '体验项目');
			$form->text('rcBuyTreatment', '购买疗程');
			$form->text('rcOtherCure', '接受过的其它治疗');
			$form->text('rcAllergy', '过敏史');
			$form->text('rcOftenTake', '经常服用的药品/保健品');
			$form->text('rcNowTake', '正在服用的药品/保健品');
			
			$states = [
			    'on'  => ['value' => 1, 'text' => '是', 'color' => 'success'],
			    'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
			];
			
			$form->switch('rcIsSkinInfection', '皮肤是否感染')->states($states);
			$form->switch('rcIsOpenWounds', '有无开放伤')->states($states);
			$form->switch('rcIsPregnancy', '是否怀孕')->states($states);
			$form->switch('rcIsRecentExposure', '近期被日光暴晒')->states($states);
			$form->switch('rcIsAvoidExposure', '治疗后能否避免日光暴晒')->states($states);
			$form->switch('rcIsSkinDisease', '是否患有癫痫')->states($states);
			$form->switch('rcIsScarSkin', '是否疤痕体质')->states($states);
			$form->switch('rcIsHirsutism', '是否先天性多毛')->states($states);
			
			$form->date('rcRecordDate', '登记时间')->default($today);
			$form->text('rcRecordName', '登记人');
			
			$form->saved(function (Form $form) {
			
			    $rcId = $form->model()->rcId;
				DB::table('record_consult')->where('rcId', $rcId)->update(['createUser'=> Admin::user()->name, 'sId' => Admin::user()->sId]);
			});
			
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
			    $footer->disableCreatingCheck();
			
			});
        });
    }
}
